package com.booking.service.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.booking.bean.Foodorder;
import com.booking.bean.Seller;
import com.booking.bean.Shop;
import com.booking.dao.OrderDAO;
import com.booking.dao.SellerDAO;
import com.booking.dao.ShopDAO;
import com.booking.service.OrderService;


@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderDAO orderDAO ;
	@Autowired
	private ShopDAO shopDAO ;
	@Autowired
	private SellerDAO sellerDAO ;
 
	
	@Override
	public BigDecimal cluTotal(Foodorder forder) {
		BigDecimal total = new BigDecimal(0.00);
		for(Shop sorder : forder.getShop()) {
			total = total.add(sorder.getPrice().multiply(new BigDecimal(sorder.getNum())));
		}
		return total;
		
	}

	@Override
	public String submitOrder(Foodorder forder) {
		Date date = new Date();
		String result = null;
		Timestamp timestamp=new Timestamp(date.getTime());  
		forder.setOrderdate(timestamp);
		
		int id = orderDAO.createOrder(forder);
		
		if(id != 0 ){
			for(Shop temp:forder.getShop()){
				temp.setOrderid(id);
				result = shopDAO.createShop(temp); 
			}
		}
		
		return result; 
	}

	@Override
	public List<Foodorder> queryOrder(int status,int id) {
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
		
		List<Foodorder> result = orderDAO.queryOrder(status, id);
		
		for(Foodorder order:result){
			Seller seller = sellerDAO.querySeller(order.getSellerid());
			order.setDate(df.format(order.getOrderdate()));
			order.setSellername(seller.getSellername());
		}
		

		return result;
	}

	@Override
	public void DeleteOrder(int id) {
		orderDAO.deleteOrder(id);
	}

	@Override
	public List<Foodorder> queryNoneOrder(int status, int id) {
		List<Foodorder> result = orderDAO.queryNoneOrder(status,id);
		return result;
	}

	@Override
	public void doneByIds(String ids) {
		orderDAO.doneByIds(ids);
		
	}

}
